from django.db import models
from django.contrib.auth.models import User
from django.utils.translation import gettext_lazy as _

class Tag(models.Model):
    """
    规则标签模型，用于对规则进行分类和筛选
    """
    CATEGORY_CHOICES = (
        ('language', _('编程语言')),
        ('scenario', _('使用场景')),
    )
    
    name = models.CharField(_('标签名称'), max_length=50, unique=True)
    category = models.CharField(_('标签分类'), max_length=20, choices=CATEGORY_CHOICES)
    created_at = models.DateTimeField(_('创建时间'), auto_now_add=True)
    
    class Meta:
        verbose_name = _('标签')
        verbose_name_plural = _('标签')
        ordering = ['category', 'name']
    
    def __str__(self):
        return self.name

class CursorRule(models.Model):
    """
    Cursor规则模型，存储规则内容和相关信息
    """
    STATUS_CHOICES = (
        ('pending', _('待审核')),
        ('approved', _('已通过')),
        ('rejected', _('已拒绝')),
    )
    
    title = models.CharField(_('规则标题'), max_length=100)
    content = models.TextField(_('规则内容'))
    tags = models.ManyToManyField(Tag, verbose_name=_('标签'), related_name='rules')
    author = models.ForeignKey(User, verbose_name=_('作者'), on_delete=models.CASCADE, related_name='rules')
    status = models.CharField(_('状态'), max_length=20, choices=STATUS_CHOICES, default='pending')
    created_at = models.DateTimeField(_('创建时间'), auto_now_add=True)
    updated_at = models.DateTimeField(_('更新时间'), auto_now=True)
    
    class Meta:
        verbose_name = _('Cursor规则')
        verbose_name_plural = _('Cursor规则')
        ordering = ['-created_at']
    
    def __str__(self):
        return self.title
